﻿function addLoadEvent(fn) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			fn();
		};
	}
}
function byClass(sClass, tag, node) {
	var elems = [];
	var elem = (node || document).getElementsByTagName(tag || "*");
	var pattern = new RegExp("\\b"+sClass+"\\b");
	for (var i = 0; i<elem.length; i++) {
		if (pattern.test(elem[i].className)) {
			elems.push(elem[i]);
		}
	}
	return elems;
}
function expand() {
	var allSub = this.parentNode.parentNode.getElementsByTagName('div');
	for (var i = 1; i<=allSub.length; i++) {
		if (allSub[i-1] == this) {
			allSub[i-1].className = "on";
			allSub[i-1].parentNode.getElementsByTagName('ul')[0].style.display = "";
		} else {
			allSub[i-1].className = "off";
			allSub[i-1].parentNode.getElementsByTagName('ul')[0].style.display = "none";
		}
	}
	return false;
}

var temp;
function hlight() {
	if (temp != null) {
		temp.getElementsByTagName("a")[0].className = "";
	}
	this.className = "now";
	temp = this.parentNode;
}
function initNav() {
	var navs = byClass("itemNav", "ul");
	for (var m = 0; m<navs.length; m++) {
		divs = navs[m].getElementsByTagName("div");
		for (var i = 0; i<divs.length; i++) {
			divs[i].onclick = expand;
			divs[i].style.cursor = "pointer";
		}
		uls = navs[m].getElementsByTagName("ul");
		for (var j = 0; j<uls.length; j++) {
			uls[j].style.display = "";
			var tempH = 430-divs.length*25;
			if (uls[j].scrollHeight>=tempH) {
				uls[j].style.height = tempH+"px";
			}
		}
		divs[0].onclick();
//		anchor = navs[m].getElementsByTagName("a");
//		for (var k = 0; k<anchor.length; k++) {
//			anchor[k].onclick = hlight;
//		}
	}
}

var sTabs = [];
function initStab() {
	var objUl = byClass("sTab", "ul")[0];
	var objLink = objUl.getElementsByTagName("a");
	for (var i = 0; i<objLink.length; i++) {
		if (objLink[i].href.indexOf("#") != -1) {
			var id = objLink[i].href.split("#")[1];
			var obj = document.getElementById(id);
			if (!obj) {
				return;
			}
			obj.relLink = objLink[i];
			sTabs[sTabs.length] = obj;
			obj.style.display = "none";
			if (i == 0) {
				objLink[i].parentNode.className = "active";
				obj.style.display = "";
			}
			objLink[i].onclick = switchTab;
		}
	}
}
function switchTab(e) {
	var evt = e || window.event;
	var evtTarget = evt.target || evt.srcElement;
	if (evtTarget.nodeType == 3) {
		evtTarget = evtTarget.parentNode;
	}
	if (evtTarget.parentNode.className.indexOf("active") != -1) {
		return false;
	}
	var id = evtTarget.href.split("#")[1];
	for (var i = 0; i<sTabs.length; i++) {
		if (sTabs[i].id != id) {
			sTabs[i].relLink.parentNode.className = "";
			sTabs[i].style.display = "none";
		} else {
			sTabs[i].relLink.parentNode.className = "active";
			sTabs[i].style.display = "";
			if(id=="chat")
			{
			    url = chatroomurl + '/chat.htm?cid=' +  mChannelIDJS + "&cname=" + mChannelNameJS + "&uid=" + mUserIDJS;
			    obj = document.getElementById('iframebbs');
			    obj.src = url;
			}
		}
	}
	return false;
}


var topvalue="";
function getvalue()
{
	return topvalue;
}
function PlayRtsp(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/rtsp_show.htm";
}
function PlaySilver(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/silver_show.htm";
}
function PlaySilverVod(id,name)
{
    //topvalue=id+'|'+encodeURIComponent(name);
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/silver_vod_show.htm";
}
function PlayRtmp(id,name)
{
	//var uniuuid=GetUniUuid(name);
	//topvalue=encodeURI(id)+'|'+encodeURI(uniuuid)+'|'+encodeURIComponent(name);
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/rtmp_show.htm";
	document.getElementById("debug").innerHTML +=topvalue;
}
function PlayVlcVod(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/vlc_vod_show.htm";
}
function PlayVlc(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/vlc_show.htm";
}
function PlayNow(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/stream_show.htm";
}
function PlayVodNow(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/vod_show.htm";
}
function PlayFlvNow(id,name)
{
	topvalue=encodeURI(id)+'|'+encodeURIComponent(name);
	document.getElementById("playerBox").src="/players/player/flv_show.htm";
}
function PlayTsNow(id, name) {
    topvalue = encodeURI(id) + '|' + encodeURIComponent(name);
    document.getElementById("playerBox").src = "/players/player/ts_show.htm";
}
function PlayTsEpgNow(id, name,cid) {
    topvalue = encodeURI(id) + '|' + cid + '|' + encodeURIComponent(name);
    document.getElementById("playerBox").src = "/players/player/ts_show_epg.htm";
}
function PlayAll(playerid,id,name,cid)
{
    switch(playerid)
    {
        case "1":
            PlayNow(id,name);
            break;
        case "2":
            PlaySilver(id,name);
            break;
        case "3":
             PlayVlc(id,name);
            break;
        case "4":
            PlayRtmp(id,name)
            break;
        case "10":
            PlayTsNow(id, name)
            break;
        case "11":
            PlayTsEpgNow(id, name, cid)
            break;
        default :
           PlayNow(id,name);
           break;                  
    }
}
function PlayAllVod(ptype,id,name)
{
    PlaySilverVod(id,name);
}

//解析xml方法直播
function LiveMenuStr(json)
{
    var menustr="";
    var mclassname = "";
    var megpname = "";
    var uuid="";
    var oddClass = false;
    var ptype="";
    var playerid = "0";
    var cid = 0;
    //var xmlDoc = GetXmlDoc(res); 
    var mitems=json.records;
    menustr += "<ul class=\"itemNav\">";
    for(i=0;i<mitems.length;i++)
    {
        var uuid="";
        //if(mitems[i]["className"]!="无插件")
         //  continue;
        //if(noplugintitle != "*" && noplugintitle.indexOf(mitems[i].childNodes[3].childNodes[0].nodeValue)<0)
        //    continue;
        if (mclassname != mitems[i]["className"])
        {
            
            if (mclassname != "")
            {
                menustr += "</ul>";
                menustr += "</li>";                            
            }
            menustr += "<li>";
            if (mclassname != "")
            {
                menustr += "<div class=\"off\">" + mitems[i]["className"] + "</div>";
                menustr += "<ul style=\"display:none;\">";
            }
            else
            {
                menustr += "<div class=\"on\">" + mitems[i]["className"] + "</div>";
                menustr += "<ul style=\"display:block;\">";
            }
            mclassname = mitems[i]["className"];
        }
        if (mitems[i]["name"] != null)
        {
            megpname = mitems[i]["name"];
        }
        else { megpname = ""; }
        try
        {
            uuid = mitems[i]["uuid"];
            ptype =  mitems[i]["ptype"];
            playerid = mitems[i]["playerid"];
            cid = mitems[i]["id"];
        }
        catch(reeor){}
        if (oddClass)
            menustr += "<li class=\"odd\"><a href=\"#\" onclick=PlayAll('"+playerid+"','"+uuid+"','"+megpname+"','"+cid+"')>" + megpname + "</a></li>";
        else
            menustr += "<li><a href=\"#\" onclick=PlayAll('" + playerid + "','" + uuid + "','" + megpname + "','" + cid + "')>" + megpname + "</a></li>";
        oddClass = !oddClass;        
    }
    if (mclassname != "")
    {
        menustr += "</ul>";
        menustr += "</li>";
    }
    //menustr +="<li><div class=\"off\">城域网系列</div><ul><li><a href=\"#\" onclick=playliveurl('http://biz.vcdn.tv380.com/getplayurl.php?5b63686e5d434e4330303030303032377c313030327c327c323030307c313030325f5b2f63686e5dVSDNSOOONERCOM00'); >子菜单</a></li></ul></li>"
    menustr += "</ul>";
    var liveshow = document.getElementById("liveshow");
    liveshow.innerHTML = menustr;
    liveshow=null;
}
function VodMenuStr(json)
{
    var menustr="";
    var mclassname = "";
    var megpname = "";
    var uuid="";
    var oddClass = false;
    var ptype="";
    //var xmlDoc = GetXmlDoc(res); 
    var mitems=json.records;
    menustr += "<ul class=\"itemNav\">";
    for(i=0;i<mitems.length;i++)
    {
        var uuid="";
        //if(noplugintitle != "*" && noplugintitle.indexOf(mitems[i].childNodes[3].childNodes[0].nodeValue)<0)
        //    continue;
        if (mclassname != mitems[i]["className"])
        {
            
            if (mclassname != "")
            {
                menustr += "</ul>";
                menustr += "</li>";                            
            }
            menustr += "<li>";
            if (mclassname != "")
            {
                menustr += "<div class=\"off\">" + mitems[i]["className"] + "</div>";
                menustr += "<ul style=\"display:none;\">";
            }
            else
            {
                menustr += "<div class=\"on\">" + mitems[i]["className"] + "</div>";
                menustr += "<ul style=\"display:block;\">";
            }
            mclassname = mitems[i]["className"];
        }
        if (mitems[i]["name"] != null)
        {
            megpname = mitems[i]["name"];
        }
        else { megpname = ""; }
        try
        {
            uuid = mitems[i]["uuid"];
            ptype =  mitems[i]["ptype"];
        }
        catch(reeor){}
        if (oddClass)
            menustr += "<li class=\"odd\"><a href=\"#\" onclick=PlayAllVod('"+ptype+"','"+uuid+"','"+megpname+"')>" + megpname + "</a></li>";
        else
            menustr += "<li><a href=\"#\" onclick=PlayAllVod('"+ptype+"','"+uuid+"','"+megpname+"')>" + megpname + "</a></li>";
        oddClass = !oddClass;        
    }
    if (mclassname != "")
    {
        menustr += "</ul>";
        menustr += "</li>";
    }
    //menustr +="<li><div class=\"off\">城域网系列</div><ul><li><a href=\"#\" onclick=playliveurl('http://biz.vcdn.tv380.com/getplayurl.php?5b63686e5d434e4330303030303032377c313030327c327c323030307c313030325f5b2f63686e5dVSDNSOOONERCOM00'); >子菜单</a></li></ul></li>"
    menustr += "</ul>";
    var liveshow = document.getElementById("ondemand");
    liveshow.innerHTML = menustr;
    liveshow=null;

}

 //根据名称获取
function GetUniUuid(name)
{
    var vodmitems=vodjson.records;
    for(i=0;i<vodmitems.length;i++)
    {
        if(vodmitems[i]["name"]==name)
            return vodmitems[i]["uuid"];
    }
    return "";
}
function initMenu()
{
    if(typeof(json)!="undefined")
    {
        LiveMenuStr(json);
        //VodMenuStr(vodjson);
        initNav();
        initStab();
    }
    
}

addLoadEvent(function(){initMenu()}); 
//addLoadEvent(initNav);
//addLoadEvent(initStab);